import { Args, Int, Mutation, Query, Resolver } from '@nestjs/graphql';
import { StudentEntity } from './student.entity';
import { PrismaService } from 'src/prisma/prisma.service';

@Resolver()
export class StudentResolver {
    constructor(
        private readonly prismaService: PrismaService
    ) { }

    @Query(() => [StudentEntity])
    getStudents() {
        return this.prismaService.student.findMany({
            include: {
                profile: true
            }
        })
    }

    @Mutation(() => Int)
    async addStudent(@Args("data", { type: () => [[String]] }) data: string[][]) {
        let count = 0;
        for (const [num_id, name] of data) {
            if (!name || !num_id) continue;
            const existingUser = await this.prismaService.student.findUnique({
                where: { num_id },
            });
            if (!existingUser) {
                await this.prismaService.student.create({
                    data: {
                        name,
                        num_id,
                        profile: {
                            create: {
                                password: num_id
                            }
                        }
                    },
                });
                count++;
            }
        }
        return count;
    }
}
